import { createStore } from 'vuex'

export default createStore({
  state: {
    //货物列表
    goodsList: new Array(),
    curState: false
  },
  getters: {
    //计算总价
    getTotalPrice(state) {
      return state.goodsList.filter(value => value.goods_state && value.goods_state === true).reduce(
        (pre, cur) => pre + cur.goods_count * cur.goods_price,
        0)
    },
    //计算总数
    getTotal(state) {
      return state.goodsList.filter(value => value.goods_state === true).reduce(
        (pre, cur) => pre + cur.goods_count,
        0)
    },
    isFullChecked(state) {
      return state.goodsList.every(v => v.goods_state)

    },
    // getState(state, id) {
    //   return state.goodsList[id - 1].goods_state
    // }
  },
  mutations: {
    //获取商品列表
    getGoods(state, payload) {
      state.goodsList = payload.list
    },
    //全选
    allCheck(state, payload) {
      state.goodsList.forEach(v => v.goods_state = payload.val)
    },
    countChange(state, payload) {
      state.goodsList[payload.id - 1].goods_count = payload.count
    },
    stateChange(state, payload) {
      state.goodsList[payload.id - 1].goods_state = payload.state
    },
  },
  actions: {

  },
  modules: {
  }
})
